<?=_asset('/js/umeditor/themes/default/css/umeditor.css',1); ?>
<?=_asset('/js/umeditor/third-party/template.min.js',1); ?>
<?=_asset('/js/umeditor/umeditor.config.js',1); ?>
<?=_asset('/js/umeditor/umeditor.min.js',1); ?>
<?=_asset('/js/umeditor/lang/zh-cn/zh-cn.js',1); ?>
<style>
    .div-setting-img{
        height: 180px;cursor: pointer;vertical-align: bottom;
        border: solid 1px #ddd;;
    }
</style>
<input type="hidden"  value="<?=$detail_object_id?>" id="txt_detail_object_id">
<div class="panel panel-default">
    <div class="panel-heading">
        <a class="goback" style="float: right" href="javascript:;"><<返回文章列表</a>
        <h3 class="panel-title">新增文章</h3>
    </div>
    <div class="panel-body" style="min-height: 400px">
        <div class="row">
            <div class="col-md-8">
                    <div class="form-group">
                        <label for="ddl_menu_id">文章分类</label>
                        <select class="form-control" id="ddl_menu_id">
                            <?php foreach ($root_menu_list as $root_menu) { ?>
                                <option value="<?= $root_menu["objectId"] ?>"  <?= $menu_id==$root_menu["objectId"]?"selected":"" ?>><?= $root_menu["menu_name"] ?></option>
                                <?php if (isset($children_menu_list[$root_menu["objectId"]])) : ?>
                                    <?php foreach ($children_menu_list[$root_menu["objectId"]] as $child_menu) { ?>
                                        <option value="<?= $child_menu["objectId"] ?>" <?= $menu_id==$child_menu["objectId"]?"selected":"" ?>><?= $child_menu["menu_name"] ?></option>
                                    <?php } ?>
                                <?php endif; ?>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="txt_title">文章标题</label>
                        <input type="text" class="form-control" value="<?=$title?>" id="txt_title">
                    </div>
                    <div class="form-group">
                        <label for="txt_createdBy">文章作者</label>
                        <input type="text" class="form-control" value="<?=$created_by?>" id="txt_createdBy">
                    </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label>缩略图(<a href="javascript:" class="uploadimage">上传图片</a> <a href="javascript:" class="clearimage">清空图片</a>)</label>
                    <?= _include('@views/admin/upload-image.php', ['img_style' => "height:180px;width:240px",'imgplace_style' => "height:180px;width:240px","img_url"=>empty($image_url)?"":$image_url]); ?>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="myEditor">文章内容</label>
            <script type="text/plain" id="myEditor" style="width:100%;height:250px;max-height:500px;"><?=$detail?></script>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" id="chk_is_top" <?=$is_top?"checked":""?>>是否置顶
            </label>
        </div>
        <button type="button" class="btn btn-primary btn-save">保存</button>
        <button type="button" class="btn btn-default goback">取消</button>
    </div>
</div>

<script>
    $(function(){
        var um = UM.getEditor('myEditor');

        $(".goback").on("click",function(){
            window.location.href=tplData.urlParams.return_url||"content-list";
        });

        $(".btn-save").on("click",function(){
            var menu_id=$("#ddl_menu_id").val();
            if($.trim(menu_id)==""){
                toastr.error("文章分类不能为空");
                $("#ddl_menu_id").focus();
                return;
            }

            var title=$("#txt_title").val();
            if($.trim(title)==""){
                toastr.error("文章标题不能为空");
                $("#txt_title").focus();
                return;
            }

            var detail=um.getContent();
            if($.trim(detail)==""){
                toastr.error("文章内容不能为空");
                UM.getEditor('myEditor').focus();
                return;
            }

            var pre_detail=um.getContentTxt();
            var pre_detail_length=pre_detail.length;
            pre_detail=pre_detail.substring(0,150);
            if(pre_detail_length>150){
                pre_detail=pre_detail+"...";
            }
            var data={
                "title":title,
                "menu_id":menu_id,
                "image_url":$("#img-url").attr("src")||"",
                "detail":detail,
                "pre_detail":pre_detail,
                "createdBy":$("#txt_createdBy").val(),
                "is_top":$("#chk_is_top").is(':checked'),
                "detail_object_id":$("#txt_detail_object_id").val()
            };

            $.ajax({
                type: "post",
                url: "/admin/save-content?mode="+(tplData.urlParams.mode||1)+"&content_id="+(tplData.urlParams.content_id||""),
                dataType: 'json',
                data: {data:MyJson.toString(data)},
                beforeSend: function () {
                    $(".btn-save").val("保存中").attr("disabled","disabled");
                },
                success: function(msg){
                    $(".btn-save").val("保存").removeAttr("disabled");
                    toastr.success( "保存成功");
                    window.location.href=tplData.urlParams.return_url||"content-list";
                },
                error:function(){
                    $(".btn-save").val("保存").removeAttr("disabled");
                    toastr.error( "保存失败");
                    window.location.href=tplData.urlParams.return_url||"content-list";
                }
            });
        });
    })

</script>